package com.x.mvp.appbar;

import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.x.mvp.R;


public abstract class AppBarBuilder {
    public static final int TYPE_MAIN_FRAGMENT = 0;
    public static final int TYPE_SECOND_FRAGMENT = 1;
    private View.OnClickListener onNavigationClickListener;
    private View.OnClickListener onRightClickListener;
    private View.OnClickListener onLeftClickListener;
    private int menuResId;
    private Toolbar.OnMenuItemClickListener menuItemClickListener;
    private int type;
    private int backGroundColor = -1;

    protected void initView(Toolbar toolbar, LinearLayout centerSpace, LinearLayout rightSpace, LinearLayout leftSpace) {
        LayoutInflater layoutInflater = LayoutInflater.from(centerSpace.getContext());
        if (getCenterSpace(layoutInflater) != null) {
            centerSpace.removeAllViews();
            centerSpace.addView(getCenterSpace(layoutInflater));
        }

        if (getRightSpace(layoutInflater) != null) {
            rightSpace.removeAllViews();
            rightSpace.addView(getRightSpace(layoutInflater));
            if (onRightClickListener != null) {
                rightSpace.setOnClickListener(onRightClickListener);
            }
        }

        if (getLeftSpace(layoutInflater) != null) {
            leftSpace.removeAllViews();
            leftSpace.addView(getLeftSpace(layoutInflater));
            if (onLeftClickListener != null) {
                leftSpace.setOnClickListener(onLeftClickListener);
            }
        }
        switch (type) {
            case TYPE_MAIN_FRAGMENT:
                break;
            case TYPE_SECOND_FRAGMENT:
                toolbar.setNavigationIcon(R.drawable.ic_back);
                toolbar.setNavigationOnClickListener(onNavigationClickListener);
                break;
            default:
                break;
        }
        if (menuResId > 0) {
            toolbar.inflateMenu(menuResId);
            toolbar.setOnMenuItemClickListener(menuItemClickListener);
        }
        if (backGroundColor != -1) {
            toolbar.setBackgroundResource(backGroundColor);
        }
    }

    protected abstract View getCenterSpace(LayoutInflater layoutInflater);

    protected abstract View getRightSpace(LayoutInflater layoutInflater);

    protected abstract View getLeftSpace(LayoutInflater layoutInflater);

    public AppBarBuilder setMenuResId(int resId) {
        menuResId = resId;
        return this;
    }

    public AppBarBuilder setOnMenuClickListener(Toolbar.OnMenuItemClickListener onClickListener) {
        menuItemClickListener = onClickListener;
        return this;
    }

    public AppBarBuilder setType(int type) {
        this.type = type;
        return this;
    }

    public AppBarBuilder setNavigationOnClickListener(View.OnClickListener onClickListener) {
        onNavigationClickListener = onClickListener;
        return this;
    }

    public AppBarBuilder setBackGroundColor(int color) {
        this.backGroundColor = color;
        return this;
    }

    public AppBarBuilder setOnRightClickListener(View.OnClickListener onRightClickListener) {
        this.onRightClickListener = onRightClickListener;
        return this;
    }

    public AppBarBuilder setOnLeftClickListener(View.OnClickListener onLeftClickListener) {
        this.onLeftClickListener = onLeftClickListener;
        return this;
    }

    public TransAppBarFragment builder() {
        TransAppBarFragment transAppBarFragment = new TransAppBarFragment();
        transAppBarFragment.setAppBarBuilder(this);
        return transAppBarFragment;
    }
}
